<?php

declare(strict_types=1);

namespace app\admin\controller;

use think\App;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Config;
class Base
{
    protected $app;
    protected $request;
    protected $admin;
    protected $site = [];

    public function __construct(App $app)
    {
        $this->app = $app;
        $this->request = $this->app->request;
        $this->initialize();
    }
    protected function initialize()
    {
        $this->checkAuth();
        $this->getSystem();
    }

    protected function checkAuth()
    {
        $this->admin = \app\admin\model\Admin::with('roles')->withoutField('password,salt')->find($this->request->uid);
        if ($this->admin->status==0) {
            return $this->error('账号己锁定,请联系管理员');
        }
    }

    protected function getSystem()
    {
        if (!$this->site = Cache::get('sitepro')) {
            $this->site = Db::name('sitepro')->where('id', '1')->find();
            Cache::set('sitepro', $this->site);
        }
        $this->site['admin'] = $this->admin;
        $this->site['actions']=Db::name('actions')->select();
        $this->site['sysinfo'] = Config::get('app.sysModel');
    }


    public function success($msg = '', $data = [])
    {
        return json(['code' => 1, 'msg' => $msg, 'data' => $data]);
    }
    public function error($msg = '')
    {
        return json(['code' => 0, 'msg' => $msg]);
    }
}
